package com.selenium;

import com.selenium.bean.FetchCorpVO;
import com.selenium.service.ThirdpartnarService;
import com.selenium.service.ThirdpartnarServiceImp;
import com.worktool.RestExecutor;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Set;

/**
 * 设置代运营主类
 * */
public class Dyy {


    public static WebDriver driver;

    public static String corpAndUserFilePath = "C:/Users/domains90/Desktop/添加代运营.txt";

    public RestTemplate restTemplate = RestExecutor.build();

    public static Set<Cookie> cookie;

    static {
        System.getProperties().setProperty("webdriver.chrome.driver", Config.driverPath);
        driver = new ChromeDriver(Config.op);
    }


    public static void main(String[] args) {
        Dyy test2 = new Dyy();
        test2.run();
    }


    public void run(){

        driver.get("https://open.work.weixin.qq.com/wwopen/login");
        //driver.manage().window().maximize();

        boolean slogin = true;

        while (slogin){
            slogin = !this.sLogin();
            this.waitTime(1000L);
            System.err.println("继续查找");
        }

        System.err.println("登入成功");

        // 登录后，等待页面加载或执行其他操作
        // 这里假设Token存储在一个名为"token"的Cookie中
//        for(Cookie cookie:driver.manage().getCookies()){
//            System.err.println(cookie.getName());
//            System.err.println(cookie.getValue());
//        }
        cookie = driver.manage().getCookies();
        ThirdpartnarService op = new ThirdpartnarServiceImp(CookieUtils.simpleCookiebean(cookie));

        //最小化
        driver.manage().window().minimize();

        try (InputStreamReader read = new InputStreamReader(new FileInputStream(corpAndUserFilePath), "UTF-8");){
            // 考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt ;
            while ((lineTxt = bufferedReader.readLine()) != null) {

                if(StringUtils.isNotBlank(lineTxt)){
                    String[] info = lineTxt.split(",");
                    String keyWord = info[0];
                    String userName = info[1];

                    FetchCorpVO corp = op.fetchCorp(keyWord);
                    try {
                        op.addThirdUser(corp,userName);
                    }catch (Exception e){
                        System.err.println(e.getMessage());
                    }
                    this.waitTime(1000L);//等待1s
                }

            }

        }catch (Exception e) {
            e.printStackTrace();
        }




    }

    private boolean sLogin(){
        try {
            return driver.findElement(By.className("index_greeting")).isDisplayed();
        }catch (Exception e){
            return false;
        }

    }

    /**
     * 强制等待
     * */
    private void waitTime(Long se)  {
        try {
            Thread.sleep(se);
        } catch (InterruptedException e) {
            System.err.println("等待异常");
            return;
        }
    }


}
